package com.ballyza;


import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import flexjson.JSONSerializer;

@SuppressWarnings("serial")
public class BallyzaCompilerServlet extends HttpServlet {

	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String toCompile = req.getParameter("value");
		toCompile = toCompile.replaceAll("2mais2", "+");
		
		CompileResult result = new CompileResult();
		
		Compiler compiler = new Compiler(result, AnallyzerType.Lexical);
		compiler.compile(toCompile);
		
		JSONSerializer serializer = new JSONSerializer();
		String response = serializer.exclude("class").deepSerialize(result);
		
		resp.setContentType("application/json");
		resp.getWriter().println(response);
	}
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		RequestDispatcher dispatcher = req.getRequestDispatcher("/lexico.html");
		dispatcher.forward(req, resp);
	}
	
}